<?php
/**
 * Description: 获取全局配置
 * User: devforma
 * Date: 15/5/5
 * Time: 10:31
 */

require 'includes/Note.class.php';

class GetGlobalSettings implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        //获取练习说明
        $exercise = $this->getExerciseInfo();

        //登录注册客服QQ号
        $serviceQQ = getOption('service_qq');
        $serviceQQ = empty($serviceQQ) ? '' : $serviceQQ;

        //公开课咨询窗口轮询间隔,已废弃
        $heartBeat = 99999;

        //公开课营销QQ
        $marketQQ = getOption('open_course_market_qq');
        $marketQQ = empty($marketQQ) ? '' : $marketQQ;

        //review版本
        $isReview = version_cmp($queryArgs['app_version'], getOption('qb_review_version'));

        //加上版本控制
        if (($queryArgs['terminal_type'] == 'iOS_phone' && version_cmp($queryArgs['app_version'], '2.1.1') >= 0) ||
            ($queryArgs['terminal_type'] == 'android_phone' && version_cmp($queryArgs['app_version'], '1.2.1') >= 0)) {

            //模考信息
            $mokao = getOption('mokaoManage');
            if (!empty($mokao) && $mokao['available']) {
                $mock = array(
                    'type' => $mokao['type'],
                    'name' => $mokao['name']
                );
            } else {
                $mock = new stdClass();
            }
        } else {
            $mock = new stdClass();
        }

        /**注释接口中更新用户知识点信息的功能
         * if(!empty($queryArgs['user_id'])) {
            $note = new Note();
            $note->updateUserNotes($queryArgs['user_id']);
        }*/
        
        //获取新版本更新信息
        $queryArgs['app_version'] = !empty($queryArgs['app_version']) ? $queryArgs['app_version'] : '0.0';
        $newVersion = $this->getNewVersion($queryArgs['terminal_type'], $queryArgs['app_version']);

        //评价文字标签
        $tags = array();
        $starTags = getOption('star_tags');
        if (!empty($starTags) && is_array($starTags)) {
            foreach ($starTags as $index => $tag) {
                $tags[] = array(
                    'star' => intval($index),
                    'tags' => explode(',', $tag)
                );
            }
        }

        //微信广告数据追踪
        if (!empty($queryArgs['idfa'])){
            youqianIdfa('quizbank', $queryArgs['idfa']);
            weixinIdfa('quizbank', $queryArgs['idfa']);
            pandaPlayIdfa('quizbank',$queryArgs['idfa']);
        }


        return genOutput(1, array(
            'service_qq' => $serviceQQ,
            'market_qq' => $marketQQ,
            'open_course_heartbeat' => $heartBeat,
            'exercise_intro' => $exercise,
            'mock' => $mock,
            'report_share_url' => YG_MOBILE_URL . '/appShare/index.html#/appShare/pr?',
            'evaluate_share_url' => YG_MOBILE_URL . '/#/live/assessment?',
            'question_share_url' => YG_MOBILE_URL . '/appShare/index.html#/appShare/quizbank?',
            'app_ios_url' => 'http://t.cn/R2Nr9Ke',
            'app_android_url' => 'http://t.cn/RtMTKdc',
            'new_version' => $newVersion,
            'is_review' => $isReview != -1,
            'latest_notify' => $this->getLatestNotify(),
            'rate_tags' => $tags,
            'course_share_url' => YG_MOBILE_URL . '/terminalType/shareCourse.html?course_id='
        ));
    }

    /**
     * 版本更新配置
     *
     * @param string $terminalType 设备类型
     * @param string $appVersion app版本号
     * @return array|stdClass
     */
    private function getNewVersion($terminalType, $appVersion) {
        $upgradeLogs = getAppUpgradeLog('quizbank', $terminalType === 'android_phone' ? 'Android' : 'iOS', $appVersion);

        if (!empty($upgradeLogs))
            return array(
                'app_version'  => $upgradeLogs->app_version,
                'size'         => $upgradeLogs->package_size,
                'notice_text'  => $upgradeLogs->content,
                'force_update' => (bool)$upgradeLogs->force_upgrade,
                'target_url'   => !empty($upgradeLogs->package_url) ? $upgradeLogs->package_url : ''
            );

        return new stdClass();
    }


    /**
     * 获取练习说明
     * @return array
     */
    private function getExerciseInfo() {
        global $cmsdb;

        $cacheKey = 'qb:getExerciseInfo';

        if (($value = cacheGet($cacheKey)) == null) {
            $value = array();

            $typeArray = array(
                '天天模考' => 'mokao',
                '整卷练习' => 'entire',
                '快速练习' => 'auto',
                '专项练习' => 'note',
                '错题练习' => 'error',
                '收藏练习' => 'collect',
                '估分' => 'evaluate',
                '模考' => 'mock'
            );

            $data = $cmsdb->getResults('SELECT * FROM `qb_paper_type` WHERE 1');
            foreach ($data as $d) {
                $value[] = array(
                    'type' => $typeArray[$d->type_name],
                    'name' => $d->type_name,
                    'intro' => $d->remark
                );
            }

            cacheAdd($cacheKey, $value, 3600*48);
        }

        return $value;
    }

    /**
     * 获取最新的通知id，用于app端小红点的展示
     */
    private function getLatestNotify() {
        global $cmsdb;

        $cacheKey = 'func:getLatestNotify:quizbank';
        if (($value = cacheGet($cacheKey)) == null) {
            $sql = 'SELECT `id` FROM `qb_notify` WHERE `app_type`="quizbank" ORDER BY `id` DESC LIMIT 1';
            $value = $cmsdb->getVar($sql);

            cacheAdd($cacheKey, $value, 10*60);
        }

        return intval($value);
    }
}